package com.app.view.component.room 
{
	import flash.display.SimpleButton;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import org.component.Button;
	
	import com.greensock.TweenLite;
	
	import com.peachcastle.events.SeatEvent;
	import com.peachcastle.core.Display;
	
	import com.app.model.data.UserData;
	
	import com.peachcastle.debug.Log;
	
	/**
	 * ...
	 * @author kevin
	 */
	public class PlayerSeats extends Display
	{
		private static const SEAT_MAX_COUNT:int = 8;
		
		private var _seats:Array = null;
		private var _seatsBtn:Array = null;

		private var _tween:TweenLite = null;
		
		private var _index:int = 0;
		
		private var _paiList:MovieClip = null;
		private var _paiListFirst:Point = null;
		private var _paiListEnd:Point = null;
		
		private var _paisBg:Array = [];
		
		public function PlayerSeats(ui:MovieClip = null) 
		{
			super(ui);
		}
		
		override protected function initialization():void 
		{
			super.initialization();
			
			_seats = [];
			_seatsBtn = [];
			for (var i:int = 1; i <= SEAT_MAX_COUNT; i++)
			{
				var item:SeatItem = new SeatItem(assest['seatItem' + i]);
				_seats[i] = item;
				item = null;

				_paisBg[i] = new Array();
				for (var j:int = 1; j <= 4; j++ )
				{
					_paisBg[i][j] = assest['seat' + i + 'bg' + j];
					assest['seat' + i + 'bg' + j].visible = false;
				}
				
				_seatsBtn[i] = new Button(assest['btn_sit_' + i]);
			}
			
			_paiList = assest['paiList'];
			_paiListFirst = new Point(_paiList.x, _paiList.y);
			clearPaiList();
		}
		
		override protected function configEventListeners():void 
		{
			super.configEventListeners();

			for (var i:int = 1; i <= SEAT_MAX_COUNT; i++)
			{
				_seatsBtn[i].addEventListener(MouseEvent.CLICK, onSeatClick);
			}
		}
		
		public function setPailistEndPoint(p:Point):void
		{
			_paiListEnd = p;
		}
		
		private function onSeatClick(e:MouseEvent):void
		{
			var evt:SeatEvent = new SeatEvent(SeatEvent.SEAT_DOWN);
			evt.seatid = parseInt(e.target.assest.name.substr(e.target.assest.name.length - 1));
			
			Log.debug("onSeatClick", evt.seatid);
			
			dispatchEvent(evt);
			evt = null;
		}
		
		public function startClock(seats:Array, time:int, isIncludeBanker:Boolean = false):void
		{
			for each(var i:int in seats)
			{
				if (!isIncludeBanker && i == 1)
				{
					continue;
				}
				
				SeatItem(_seats[i]).startClock(time);
			}
		}
		
		public function stopClock(seats:int):void
		{
			SeatItem(_seats[seats]).stopClock();
		}
		
		public function stopAllClock():void
		{
			for (var i:int = 1; i <= SEAT_MAX_COUNT; i++)
			{
				SeatItem(_seats[i]).stopClock();
			}
		}
		
		public function setSeats(b:Boolean = false, seats:Array = null):void
		{
			var i:int = 1;
			if (seats && seats.length > 0)
			{
				for (i = 1; i <= SEAT_MAX_COUNT; i++)
				{
					if (seats.indexOf(i) == -1)
					{
						if (b == true)
						{
							_seatsBtn[i].show();
						}else
						{
							_seatsBtn[i].hide();
						}
					}
				}
			}else
			{
				for (i = 1; i <= SEAT_MAX_COUNT; i++)
				{
					if (b == true)
					{
						_seatsBtn[i].show();
					}else
					{
						_seatsBtn[i].hide();
					}
				}
			}
		}
		
		public function addPlayer(seatid:int = 0, data:UserData = null):void
		{
			SeatItem(_seats[seatid]).addPlayer(data);
			_seatsBtn[seatid].hide();
			_index++;
		}
		
		public function removePlayer(seatid:int = 0):void
		{
			if (seatid < 1 || seatid > SEAT_MAX_COUNT) return;
	
			resetSeat(seatid);
			SeatItem(_seats[seatid]).removePlayer();
		}
		
		public function setPaisBg(seatid:int, isShow:Boolean = false):void
		{
			for (var j:int = 1; j <= 4; j++ )
			{
				_paisBg[seatid][j].visible = isShow;
			}
		}
		
		public function showPaijiuValues(seatid:int, cardIndex:int, count:int, data:Array, isSelf:Boolean):void
		{
			SeatItem(_seats[seatid]).showPaijiuValues(cardIndex, count, data, isSelf);
		}
		
		public function updateSeatInfo(seatid:int, data:UserData = null):void
		{
			SeatItem(_seats[seatid]).update(data);
		}
		
		public function changePaijiuStatus(seatid:int, isActive:Boolean):void
		{
			SeatItem(_seats[seatid]).changePaijiuStatus(isActive);
		}
		
		public function getPaijiuPosition(seatid:int):String
		{
			return SeatItem(_seats[seatid]).getPaijiuPosition();
		}
		
		public function resetSeats():void
		{
			for (var i:int = 1; i <= SEAT_MAX_COUNT; i++)
			{
				resetSeat(i);
				setGameStatus(i, 'init');
			}
		}
		
		public function resetSeat(seatid:int):void
		{
			setOperation(seatid, 'banker', 'init');
			setOperation(seatid, 'playr', 'init');
			
			SeatItem(_seats[seatid]).hidePaijius();
			SeatItem(_seats[seatid]).setTip(false);
			
			for (var j:int = 1; j <= 4; j++)
			{
				if (_paisBg[seatid][j].visible == true)
				{
					_paisBg[seatid][j].visible = false;
				}
			}
		}
		
		public function clearPaiList():void
		{
			for (var i:int = 1; i <= 16; i++)
			{
				if (MovieClip(_paiList["pai" + i]).visible == false)
				{
					MovieClip(_paiList["pai" + i]).visible = true;
				}
			}
			_paiList.alpha = 0;
			_paiList.scaleX = 1;
			_paiList.scaleY = 1;
			_paiList.x = _paiListFirst.x;
			_paiList.y = _paiListFirst.y;
		}
		
		public function sendCard(selfSeatid:int, startSeatid:int, players:Array, paisData:Array, clockTime:int, showPourSeatid:int):void
		{
			TweenLite.to(_paiList, .2, { alpha: 100, onComplete:autoSendCard, onCompleteParams: [selfSeatid, startSeatid, players, paisData, clockTime, showPourSeatid] } );
		}
		
		private var _sendCount:int = 0;
		private var _sendParams:Array = [];
		private var _clockTime:int = 0;
		private var _showPourSeatid:int = 0;
		/**
		 * 自动发牌
		 */
		private function autoSendCard(selfSeatid:int, startSeatid:int, players:Array, paisData:Array, clockTime:int, showPourSeatid:int):void
		{	
			_sendCount = 0;
			_sendParams = [selfSeatid, players, paisData];
			_clockTime = clockTime;
			_showPourSeatid = showPourSeatid;
			
			sendSingleCard();					
		}
		
		/**
		 * 发单张牌
		 */
		private function sendSingleCard():void
		{
			var selfSeatid:int = _sendParams[0];
			var players:Array = _sendParams[1];
			var paisData:Object = _sendParams[2];
			if (_sendCount < players.length * 2) 
			{
				TweenLite.to(_paiList["pai" + (_sendCount + 1)], .3, { onComplete:sendCardHandler, onCompleteParams:[selfSeatid, players, paisData, _paiList["pai" + (_sendCount + 1)]] } );	
			}else
			{
				TweenLite.to(_paiList, .2, { x: _paiListEnd.x, y: _paiListEnd.y, scaleX:.5, scaleY:.5 } );
				startClock(players, _clockTime);
				if (_showPourSeatid > 1) setOperation(_showPourSeatid, 'playr', 'callPour', true);
				if (selfSeatid > 0) SeatItem(_seats[selfSeatid]).showTips();
			}
			
		}
		
		/**
		 * 发牌
		 */
		private function sendCardHandler(selfSeatid:int, players:Array, paisData:Array, mc:MovieClip):void 
		{						
			var cardIndex:int = 0;
			var seatid:int = players[_sendCount % players.length];
			if (_sendCount + 1 > players.length)
			{
				cardIndex = 3;
			}else
			{
				cardIndex = 1;	
			}

			SeatItem(_seats[seatid]).sendCard(mc, seatid, cardIndex, paisData[seatid], selfSeatid == seatid, sendSingleCard, _paisBg, _paiList);

			_sendParams = [selfSeatid, players, paisData];
			_sendCount++;
		}	
		
		public function showResult(seatid:int, data:Array, isShowBig:Boolean = false):void
		{
			SeatItem(_seats[seatid]).showResult(data, isShowBig);
			for (var i:int = 1; i <= 4; i++)
			{
				if (_paisBg[seatid][i].visible == true)
				{
					_paisBg[seatid][i].visible = false;
				}
			}
		}
		
		public function setOperation(seatid:int, user:String, act:String, status:Boolean = false, minPour:int = -1, maxPour:int = -1):void
		{
			if (user == 'banker')
			{
				switch(act)
				{
					case 'startGame':
						if (status)
						{
							SeatItem(_seats[seatid]).bankerOpt.addEventListener(SeatEvent.SEAT_START_GAME, onStartGameClick);
						}else
						{
							SeatItem(_seats[seatid]).bankerOpt.removeEventListener(SeatEvent.SEAT_START_GAME, onStartGameClick);
						}
					
						break;
					
					case 'pk':
						if (status)
						{
							SeatItem(_seats[seatid]).bankerOpt.addEventListener(SeatEvent.START_PK, onPkClick);
						}else
						{
							SeatItem(_seats[seatid]).bankerOpt.removeEventListener(SeatEvent.START_PK, onPkClick);
						}
						
						break;
						
					case 'init':
						SeatItem(_seats[seatid]).bankerOpt.removeEventListener(SeatEvent.SEAT_START_GAME, onStartGameClick);
						SeatItem(_seats[seatid]).bankerOpt.removeEventListener(SeatEvent.START_PK, onPkClick);
						
						break;
				}
				
				SeatItem(_seats[seatid]).bankerOpt.setOperation(act, status);
			}else if (user == 'playr')
			{
				switch(act)
				{
					case 'basePour':
						if (status)
						{
							SeatItem(_seats[seatid]).playrOpt.addEventListener(SeatEvent.SEAT_BASE_POUR, onBasePourClick);
						}else
						{
							SeatItem(_seats[seatid]).playrOpt.removeEventListener(SeatEvent.SEAT_BASE_POUR, onBasePourClick);
						}
	
						break;
						
					case 'callPour':
						if (status)
						{
							SeatItem(_seats[seatid]).playrOpt.addEventListener(SeatEvent.SEAT_POUR, onPourClick);
						}else
						{
							SeatItem(_seats[seatid]).playrOpt.removeEventListener(SeatEvent.SEAT_POUR, onPourClick);
						}
						
						break;
						
					case 'init':
						SeatItem(_seats[seatid]).playrOpt.removeEventListener(SeatEvent.SEAT_BASE_POUR, onBasePourClick);
						SeatItem(_seats[seatid]).playrOpt.removeEventListener(SeatEvent.SEAT_POUR, onPourClick);
						
						break;
				}

				SeatItem(_seats[seatid]).playrOpt.setOperation(act, status, minPour, maxPour);
			}
		}
		
		private function onStartGameClick(e:SeatEvent):void
		{
			var evt:SeatEvent = new SeatEvent(SeatEvent.SEAT_START_GAME);
			evt.seatid = e.seatid;
			dispatchEvent(evt);
			evt = null;
		}
		
		private function onBasePourClick(e:SeatEvent):void
		{
			var evt:SeatEvent = new SeatEvent(SeatEvent.SEAT_BASE_POUR);
			evt.seatid = e.seatid;
			evt.state = e.state;
			evt.message = e.message;
			dispatchEvent(evt);
			evt = null;
		}
		
		private function onPourClick(e:SeatEvent):void
		{
			var evt:SeatEvent = new SeatEvent(SeatEvent.SEAT_POUR);
			evt.seatid = e.seatid;
			evt.state = e.state;
			evt.message = e.message;
			dispatchEvent(evt);
			evt = null;
		}
		
		private function onPkClick(e:SeatEvent):void
		{
			var evt:SeatEvent = new SeatEvent(SeatEvent.START_PK);
			evt.seatid = e.seatid;
			evt.state = e.state;
			dispatchEvent(evt);
			evt = null;
		}
		
		public function setGameStatus(seatid:int, status:String, b:Boolean = false):void
		{
			SeatItem(_seats[seatid]).setGameStatus(status, b);
		}
		
		public function getSeatPoint(seatid:int):Point
		{
			return new Point(_seatsBtn[seatid].x, _seatsBtn[seatid].y);
		}
		
		public function get index():int { return _index; }
	}

}